package com.office.web;

import com.office.bean.*;
import com.office.service.EmpService;
import com.office.service.LoginStatuService;
import com.office.utils.DepartBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class EmpController {

    @Autowired
    EmpService empService;
    @Autowired
    LoginStatuService loginStatuService;

    /**
     * 自动跳转
     */
    @RequestMapping({"/","/login"})
    public String loginui(){
        return "login";
    }

    /**
     * 跳转到index
     */
    @RequestMapping("/index")
    private String index(){
        return "index";
    }

//    /**
//     * 跳转Index
//     */
//    @RequestMapping("/main")
//    public String mainUI() {
//        return "main";
//    }
    /**
     * 执行登录
     */
    @RequestMapping("emp_login")
    private void login(Emp emp,String ip,String city,HttpServletResponse resp, HttpSession session) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("登录的用户是："+emp);
        System.out.println("登陆的IP是："+ip);
        System.out.println("登录的地点为："+city);
        Emp loginguser = empService.findAllEmp(emp);
        if (loginguser!=null){
            //存储用户当千的状态
            session.setAttribute("loginuser",loginguser);
            //向数据库写入用户的状态
            int addstatus = loginStatuService.addstatus(new TLoginlog(ip, loginguser.getNo(), city));
            System.out.println(addstatus>0?"登录日记插入成功":"登录日记插入失败");

            resp.getWriter().write("<script>location.href='/index';</script>");
        }else {
            resp.getWriter().write("<script>alert('用户密码或账号错误');location.href='/';</script>");
        }
    }

    /**
     * 注销登录
     */
    @RequestMapping("emp_logout")
    private String loginout(HttpSession session){
        session.invalidate();
        return "login";
    }

    /**
     * 登录查询
     */
    @RequestMapping("emp_log")
    @ResponseBody
    private List<TLoginlog> log (HttpSession session){
         Emp emp =(Emp) session.getAttribute("loginuser");
        return loginStatuService.logLists(emp.getNo());
    }
    /**
     * 跳转员工管理界面
     */
    @RequestMapping("/Emp_list")
    private String EmpList(long pageIndex, long pagesize, Model model){
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pagesize",pagesize);

        int count = empService.findallcoutEmp();//总记录条数
        List<EmpManger> empMangers = empService.findempBypage(map);//查询每页的记录数

        DepartBeanUtils<EmpManger> EmpUtils = new DepartBeanUtils<>(pageIndex, pagesize, count, empMangers);
        model.addAttribute("emputils",EmpUtils);

        return "emplist";


    }
    @RequestMapping("/emp_photoload")
    @ResponseBody
    public ImageMessage uploadimage(MultipartFile file, HttpSession session) throws IOException {
//
//        获取上传的文件名
        String filename = file.getOriginalFilename();
//        System.out.println("上传的文件名："+filename);///xxx.jpg
//        获取文件的尾缀
        String suffix = filename.split("\\.")[1];//.jpg
        String uuid = UUID.randomUUID().toString();

        //图片的新名字
        String newStingname = uuid+"."+suffix;
        //获取上传的图片位置
       String path  = session.getServletContext().getRealPath("/media/images")+"/"+newStingname;
        System.out.println("图片的路径为："+path);

        //开始上传
        file.transferTo(new File(path));

        ImageMessage imageMessage = new ImageMessage(1000, newStingname);
        return imageMessage;
    }

    @RequestMapping("/Emp_findDepart")
    @ResponseBody
    public List<TDepart> finddepart(){
//        System.out.println("进入了finddepart的控制层");
        List<TDepart> finddeparts = empService.finddeparts();
        System.out.println("找到的部门为："+finddeparts);
        return finddeparts;
    }
    @RequestMapping("/Emp_add")
    public void add(Emp emp,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        boolean addemp = empService.addemp(emp);
        System.out.println("新增的人员为："+emp);
        if (addemp){
//            resp.getWriter().write("<script>alert('更新成功，正在跳转');location.href='/Depart_list?pageindex=1&pagesize=5';</script>");

            resp.getWriter().write("<script>alert('新增成功');location.href='/Emp_list?pageIndex=1&pagesize=5';</script>");
        }else {
            resp.getWriter().write("<script>alert('新增失败');location.href='/Emp_add';</script>");
        }
    }
    @RequestMapping("/Emp_updateFlag")
    @ResponseBody
    public boolean updataFlag(int id,int flag){
        System.out.println("修改状态的id为"+id+"      "+"状态值为："+flag);
        boolean updateflag = empService.updateflag(id, flag);
        return updateflag;
    }

    @RequestMapping("/emp_findMajorAndEmpAllcount")
    @ResponseBody
    public List<Integer> findMajorAndEmpAllcount(){
//        System.out.println("dsadasdsadasdsa");
        List<Integer> majorAndEmpAllcount = empService.findMajorAndEmpAllcount();
        System.out.println("后端查询到的"+majorAndEmpAllcount);
        return majorAndEmpAllcount;
    }
}
